<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\user\model\UserModel;
use cmf\controller\HomeBaseController;
use think\Validate;

class PublicController extends HomeBaseController
{
    public function login()
    {
        return $this->fetch(':login');
    }

    /**
     * 登录验证提交
     */
    public function doLogin()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'username'  => 'require|mobile',
                'captcha' => 'require',
            ]);
            $validate->message([
                'username.require' => '手机号不能为空',
                'captcha.require'  => '验证码不能为空',
            ]);

            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $errMsg = cmf_check_verification_code($data['username'], $data['captcha']);
            if (!empty($errMsg)) {
                $this->error($errMsg);
            }

            $userModel         = new UserModel();

            $user['mobile'] = $data['username'];
            $log            = $userModel->doMobile($user);

            $session_login_http_referer = session('login_http_referer');
            $redirect                   = empty($session_login_http_referer) ? $this->request->root() : $session_login_http_referer;
            switch ($log) {
                case 0:
                    cmf_user_action('login');
                    $this->success('登录成功', '/');
                    break;
                case 1:
                    $this->error('登录密码错误');
                    break;
                case 2:
                    $this->error('账户不存在');
                    break;
                case 3:
                    $this->error('账号被禁止访问系统');
                    break;
                default :
                    $this->error('未受理的请求');
            }
        } else {
            $this->error("请求错误");
        }
    }

    public function sendVerificationCode()
    {
        $validate = new Validate([
            'username' => 'require',
        ]);

        $validate->message([
            'username.require' => '请输入手机号或邮箱!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $accountType = '';

        if (!preg_match('/^1[0-9]{10}$/', $data['username'])) {
            $this->error("请输入正确的手机或者邮箱格式!");
        }


        $code = cmf_get_verification_code($data['username']);

        if (empty($code)) {
            $this->error("验证码发送过多,请明天再试!");
        }



        $param  = ['mobile' => $data['username'], 'code' => $code];

        /*$result = hook_one("send_mobile_verification_code", $param);

        if ($result !== false && !empty($result['error'])) {
            $this->error($result['message']);
        }

        if ($result === false) {
            $this->error('未安装验证码发送插件,请联系管理员!');
        }*/

        cmf_verification_code_log($data['username'], $code);
        $this->success("测试验证码：$code");
        if (!empty($result['message'])) {
            $this->success($result['message']);
        } else {
            $this->success('验证码已经发送成功!');
        }




    }

}
